<?php
require_once('../classes/smarty_setup.php');
require_once('../classes/mofachi.UserBaby.php');
$smarty = new Smarty_mofachi();

@ $act = $_REQUEST[ "act" ];
if( $act == "add" ) {
	addUserBaby();
} else if( $act == "remove" ) {
	removeUserBaby();
} else if( $act == "get" ) {
	getUserBaby( $smarty );
} else if( $act == "update" ) {
	updateUserBaby();
}
$smarty->assign( 'act', $act );

listUserBaby( $smarty );
$smarty->display('t04_user_baby.htm');

function listUserBaby( $smarty ) {
	$result = UserBaby::listData( 0, 20 );
	$smarty->assign( 'res', $result );
}
function addUserBaby() {
	@ $user_id = $_POST[ 'user_id' ];
	@ $baby_id = $_POST[ 'baby_id' ];
	@ $relation = $_POST[ 'relation' ];
	if( !$user_id || !$baby_id || !$relation )
		return;
	
	$ub = new UserBaby();
	$ub->user_id = $user_id;
	$ub->baby_id = $baby_id;
	$ub->relation = $relation;
	$ub->add();
}
function removeUserBaby() {
	@ $delist = $_POST[ "delist" ];
	if( !$delist )
		return;

	$id_array = explode( ",", $delist );
	foreach( $id_array as $key => $value ) {
		$id = explode( ":", $value );
		$ub = new UserBaby();
		$ub->user_id = $id[ 0 ];
		$ub->baby_id = $id[ 1 ];
		$ub->remove();
	}
}
function getUserBaby( $smarty ) {	
	@ $uid = $_GET[ "uid" ];	
	@ $bid = $_GET[ "bid" ];
	if( !$uid || !$bid )
		return;
	
	$ub = new UserBaby();
	$ub->user_id = $uid;
	$ub->baby_id = $bid;
	$ub->loadById();
	$smarty->assign( 'row', $ub );
}
function updateUserBaby() {
	@ $uid = $_POST[ 'uid' ];
	@ $bid = $_POST[ 'bid' ];
	@ $relation = $_POST[ 'relation' ];
	if( !$uid || !$bid || !$relation )
		return;

	$ub = new UserBaby();
	$ub->user_id = $uid;
	$ub->baby_id = $bid;
	$ub->relation= $relation;
	$ub->update();
}
?>